.cherry-markdown {
  word-break: break-all;
  color: var(--md-paragraph-color);
  background-color: var(--base-previewer-bg);
  h1,
  h2,
  h3,
  h4,
  h5,
  h6,
  .h1,
  .h2,
  .h3,
  .h4,
  .h5,
  .h6 {
    font-family: inherit;
    font-weight: 700;
    line-height: 1.1;
    color: var(--md-heading-color);
  }

  h1 small,
  h2 small,
  h3 small,
  h4 small,
  h5 small,
  h6 small,
  .h1 small,
  .h2 small,
  .h3 small,
  .h4 small,
  .h5 small,
  .h6 small,
  h1 .small,
  h2 .small,
  h3 .small,
  h4 .small,
  h5 .small,
  h6 .small,
  .h1 .small,
  .h2 .small,
  .h3 .small,
  .h4 .small,
  .h5 .small,
  .h6 .small {
    font-weight: normal;
    line-height: 1;
    color: var(--oc-gray-5);
  }

  h1,
  h2,
  h3 {
    margin-top: 30px;
    margin-bottom: var(--spacing-lg);
  }

  h1 small,
  h2 small,
  h3 small,
  h1 .small,
  h2 .small,
  h3 .small {
    font-size: 65%;
  }

  h4,
  h5,
  h6 {
    margin-top: var(--spacing-md);
    margin-bottom: var(--spacing-md);
  }

  h4 small,
  h5 small,
  h6 small,
  h4 .small,
  h5 .small,
  h6 .small {
    font-size: 75%;
  }

  h1,
  .h1 {
    font-size: 2em;
  }

  h2,
  .h2 {
    font-size: 1.5em;
  }

  h3,
  .h3 {
    font-size: 1.25em;
  }

  h4,
  .h4 {
    font-size: 1em;
  }

  h5,
  .h5 {
    font-size: 0.875em;
  }

  h6,
  .h6 {
    font-size: 0.85em;
  }

  b,
  strong {
    font-weight: bold;
  }

  ul,
  ol {
    padding-left: var(--spacing-xl);
    margin-bottom: var(--spacing-lg);

    ul,
    ol {
      margin-bottom: 0;
    }

    li {
      list-style: inherit;
      p {
        margin: 0;
      }
    }
  }

  div {
    ul,
    ol {
      margin-bottom: 0;
    }
  }

  hr {
    height: 0;
    border: 0;
    border-top: 1px solid var(--md-hr-border);
    margin: var(--spacing-lg) 0;
    box-sizing: content-box;
    overflow: visible;
  }

  kbd {
    border: 1px solid var(--base-border-color);
    border-radius: 4px;
    padding: 1px 2px;
    box-sizing: border-box;
    box-shadow: inset 0px -1px var(--base-border-color);
    font-size: 0.85rem;
  }

  table {
    border-collapse: collapse;
    color: var(--md-paragraph-color);
    th,
    td {
      border: 1px solid var(--md-table-border);
      padding: 0.2em 0.4em;
      min-width: 100px;
    }
    th {
      background-color: var(--md-inline-code-bg);
    }
  }

  .link-quote {
    color: var(--md-link-color);
  }

  a {
    color: var(--md-link-color);
    position: relative;
    text-decoration: none;

    &[target='_blank'] {
      padding: 0 2px;

      &::after {
        content: '\EA10';
        font-size: var(--font-size-xs);
        font-family: 'ch-icon';
        margin: 0 2px;
      }
    }

    &:hover {
      text-decoration: underline;
      color: var(--md-link-hover-color);
    }
  }

  em {
    font-style: italic;
    &.cherry-right-padding {
      padding-right: 0.2em;
    }
  }

  sup {
    vertical-align: super;
  }

  sub {
    vertical-align: sub;
  }

  figure {
    overflow-x: auto;
    margin: var(--spacing-lg) 0;
  }

  p,
  pre,
  blockquote,
  table {
    margin: 0 0 var(--spacing-lg);
  }

  blockquote {
    color: var(--md-paragraph-color);
    padding: 10px 15px;
    border-left: 10px solid var(--md-hr-border);
    background: var(--md-blockquote-bg);
    p, blockquote, table, pre, ul, ol{
      margin: 0;
    }
  }

  pre {
    padding: var(--spacing-lg);
    overflow: auto;
    font-size: 85%;
    line-height: 1.45;
    background-color: #f6f8fa;
    border-radius: 6px;
  }

  div[data-type='codeBlock'] {
    display: inline-block;
    width: 100%;
    box-sizing: border-box;
    border-radius: 2px;
    margin-bottom: var(--spacing-lg);
    font-size: var(--font-size-sm);
    overflow-x: auto;

    & > pre {
      margin: 0;
      code[class*='language-'] {
        counter-reset: line;
        &.wrap {
          white-space: pre-wrap;
        }
        .code-line {
          display: inline-block;
          position: relative;
          padding-left: 3em;
          height: 1.3em;
          line-height: 2em;
          &:before {
            counter-increment: line;
            content: counter(line);
            margin-right: 1em;
            position: absolute;
            left: 0;
          }
          &:last-child {
            margin-bottom: 0;
          }
        }
      }
    }
  }

  /* Inline code */
  :not(pre) > code {
    padding: 0.1em;
    border-radius: 0.3em;
    white-space: normal;
    color: var(--md-inline-code-color);
    background-color: var(--md-inline-code-bg);
    border: 1px solid var(--md-hr-border);

    [data-inline-code-theme='black'] & {
      color: var(--base-font-color);
      background-color: var(--md-inline-code-bg);
    }
  }

  a.anchor:before {
    content: '\a7'; // §
    text-decoration: none;
    width: 15px;
    font-size: 0.5em;
    vertical-align: middle;
    display: inline-block;
    text-align: center;
    margin-left: -15px;
  }

  .toc {
    margin-bottom: var(--spacing-lg);
    padding-left: 0;

    // 容器样式
    background-color: var(--md-toc-bg);
    border: var(--border-width-1) solid var(--md-toc-border-color);
    border-radius: var(--md-toc-radius);
    padding: var(--md-toc-padding);

    .toc-title {
      font-size: var(--font-size-2xl);
      margin-bottom: var(--spacing-sm);
      font-weight: var(--font-weight-bold);
      color: var(--md-toc-title-color);
    }

    .toc-li {
      list-style: none;
      margin: 0;
      display: flex;
      align-items: center;
      border-left: var(--md-toc-indicator-width) solid var(--md-toc-indicator-color);
      padding-left: var(--md-toc-indicator-gap);
      transition: border-color 50ms ease-out;

      .cherry-toc-in-blockquote {
        font-size: 0.6em;
        margin-top: -0.4em;
        padding: 0 0 0 var(--spacing-sm);
        color: var(--md-toc-indicator-color);
      }

      a {
        text-decoration: none;
        color: var(--md-toc-link-color);
        flex-grow: 1;
        padding: var(--spacing-xs) var(--spacing-sm);
        border-radius: var(--md-toc-link-radius);
        transition: color 50ms ease-out, background-color 50ms ease-out;

        &:hover {
          color: var(--md-toc-link-hover-color);
          background-color: var(--md-toc-link-hover-bg);
        }

        &:active {
          background-color: var(--md-toc-link-active-bg);
        }

        &.level-1 {
          font-weight: var(--font-weight-semibold);
        }        
      }

      &:hover,
      &:has(a:active) {
        border-left-color: var(--md-toc-indicator-hover-color);
      }
    }
  }

  .auto-num-toc {
    counter-reset: headtoclevel1;

    .toc-li-1 {
      counter-reset: headtoclevel2;
      a:before {
        counter-increment: headtoclevel1;
        content: counter(headtoclevel1) ". "
      }
    }

    .toc-li-2 {
      counter-reset: headtoclevel3;
      a:before {
        counter-increment: headtoclevel2;
        content: counter(headtoclevel1) "." counter(headtoclevel2) ". "
      }
    }

    .toc-li-3 {
      counter-reset: headtoclevel4;
      a:before {
        counter-increment: headtoclevel3;
        content: counter(headtoclevel1) "." counter(headtoclevel2) "." counter(headtoclevel3) ". "
      }
    }

    .toc-li-4 {
      counter-reset: headtoclevel5;
      a:before {
        counter-increment: headtoclevel4;
        content: counter(headtoclevel1) "." counter(headtoclevel2) "." counter(headtoclevel3) "." counter(headtoclevel4) ". "
      }
    }

    .toc-li-5 {
      counter-reset: headtoclevel6;
      a:before {
        counter-increment: headtoclevel5;
        content: counter(headtoclevel1) "." counter(headtoclevel2) "." counter(headtoclevel3) "." counter(headtoclevel4) "." counter(headtoclevel5) ". "
      }
    }

    .toc-li-6 {
      a:before {
        counter-increment: headtoclevel6;
        content: counter(headtoclevel1) "." counter(headtoclevel2) "." counter(headtoclevel3) "." counter(headtoclevel4) "." counter(headtoclevel5) "." counter(headtoclevel6) ". "
      }
    }
  }

  .check-list-item {
    list-style: none;

    .ch-icon {
      margin: 0 6px 0 -20px;
    }
  }

  .footnote:not(a) {
    .footnote-title {
      font-size: 20px;
      margin-top: -38px;
      background-color: var(--md-inline-code-bg);
      width: 60px;
      margin-bottom: var(--spacing-lg);
    }

    padding-top: 20px;
    border-top: 1px solid var(--md-hr-border);
    margin-top: 50px;

    .one-footnote {
      color: var(--md-paragraph-color);
      margin-bottom: var(--spacing-lg);
      border-bottom: 1px dotted var(--md-hr-border);

      a.footnote-ref {
        padding: 5px;
      }
    }
  }

  .cherry-table-container {
    max-width: 100%;
    overflow-x: auto;
    .cherry-table {
      th,
      td {
        border: 1px solid var(--md-table-border);
        padding: 0.2em 0.4em;
        min-width: 100px;
      }
      th {
        white-space: nowrap;
        background-color: var(--md-inline-code-bg);
      }
    }
  }

  /* 数学表达式展示 */
  mjx-assistive-mml {
    position: absolute;
    top: 0;
    left: 0;
    clip: rect(1px, 1px, 1px, 1px);
    padding: 1px 0 0 0;
    border: 0;
  }

  &.head-num {
    // Auto Numbering for Headings
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      .anchor:before {
        width: auto;
        font-size: inherit;
        vertical-align: inherit;
        padding-right: 10px;
      }
    }

    counter-reset: level1;

    h1 {
      counter-reset: level2;
    }

    h2 {
      counter-reset: level3;
    }

    h3 {
      counter-reset: level4;
    }

    h4 {
      counter-reset: level5;
    }

    h5 {
      counter-reset: level6;
    }

    h1 .anchor:before {
      counter-increment: level1;
      content: counter(level1) '. ';
    }

    h2 .anchor:before {
      counter-increment: level2;
      content: counter(level1) '.' counter(level2) ' ';
    }

    h3 .anchor:before {
      counter-increment: level3;
      content: counter(level1) '.' counter(level2) '.' counter(level3) ' ';
    }

    h4 .anchor:before {
      counter-increment: level4;
      content: counter(level1) '.' counter(level2) '.' counter(level3) '.' counter(level4) ' ';
    }

    h5 .anchor:before {
      counter-increment: level5;
      content: counter(level1) '.' counter(level2) '.' counter(level3) '.' counter(level4) '.' counter(level5) ' ';
    }

    h6 .anchor:before {
      counter-increment: level6;
      content: counter(level1) '.' counter(level2) '.' counter(level3) '.' counter(level4) '.' counter(level5) '.'
        counter(level6) ' ';
    }
  }
}

// 表格编辑器
.cherry[class*="theme__"] .cherry-previewer-table-content-handler {
  .cherry-previewer-table-content-handler__input {
    textarea {
      background-color: var(--base-editor-bg);
      color: var(--base-font-color);
      outline-color: var(--editor-header-color);
    }
  }
}

div[data-type='codeBlock'] {
  @import 'prism/tomorrow-night';

  div[data-code-wrap='wrap'] & code[class*=language-]{
    white-space: pre-wrap;
    word-break: break-word;
  }

  [data-code-block-theme='default'] & {
    @import 'prism/default';
  }

  [data-code-block-theme='dark'] & {
    @import 'prism/dark';
  }

  [data-code-block-theme='one-dark'] & {
    @import 'prism/one-dark';
  }

  [data-code-block-theme='one-light'] & {
    @import 'prism/one-light';
  }

  [data-code-block-theme='vs-dark'] & {
    @import 'prism/vs-dark';
  }

  [data-code-block-theme='vs-light'] & {
    @import 'prism/vs-light';
  }

  [data-code-block-theme='okaidia'] & {
    @import 'prism/okaidia';
  }

  [data-code-block-theme='twilight'] & {
    @import 'prism/twilight';
  }

  [data-code-block-theme='coy'] & {
    @import 'prism/coy';
  }

  [data-code-block-theme='solarized-light'] & {
    @import 'prism/solarized-light';
  }
}

.cherry-detail {
  details {
    background: var(--accordion-bg);
    border: 1px solid var(--accordion-border);
    border-radius: calc(var(--accordion-radius) + 1px);
    box-shadow: var(--accordion-shadow);
    margin-bottom: var(--spacing-md);
    transition: box-shadow 0.2s ease;
    overflow: hidden;

    &:hover {
      box-shadow: var(--accordion-shadow);
    }

    summary {
      background: var(--accordion-summary-bg);
      color: var(--accordion-summary-color);
      padding: var(--spacing-md) var(--spacing-lg) var(--spacing-md) calc(var(--spacing-lg) + var(--spacing-xl));
      cursor: pointer;
      font-weight: 600;
      transition: background-color 0.2s ease;
      position: relative;
      user-select: none;
      border-radius: var(--accordion-radius) var(--accordion-radius) 0 0;

      &:hover {
        background: var(--accordion-summary-hover-bg);
      }

      &::marker {
        content: '';
      }

      &::before {
        content: '▼';
        position: absolute;
        left: var(--spacing-md);
        top: 50%;
        transform: translateY(-50%);
        transition: transform 0.2s ease;
      }
    }

    &[open] summary::before {
      transform: translateY(-50%) rotate(180deg);
    }

    &:not([open]) summary {
      border-radius: var(--accordion-radius);
    }

    .cherry-detail-body {
      padding: var(--spacing-lg);
      background: var(--accordion-body-bg);
      border-top: 1px solid var(--accordion-body-border);
      color: var(--accordion-body-color);
      line-height: var(--line-height-relaxed);
      border-radius: 0 0 var(--accordion-radius) var(--accordion-radius);

      > *:first-child {
        margin-top: 0;
      }

      > *:last-child {
        margin-bottom: 0;
      }
    }
  }
}

.cherry-detail__multiple {
  border-radius: calc(var(--accordion-radius) + 1px);
  // 这边的 1px 是内外圆角需要用 1px 的间隔来消除内外圆角间隙
  box-shadow: var(--accordion-shadow);
  background: var(--accordion-bg);
  border: 1px solid var(--accordion-border);
  overflow: hidden;

  details {
    margin-bottom: 0;
    border-radius: 0;
    border: none;
    border-bottom: 1px solid var(--accordion-body-border);
    box-shadow: none;

    // 第一个手风琴项的标题有顶部圆角
    &:first-child summary {
      border-radius: var(--accordion-radius) var(--accordion-radius) 0 0;
    }

    // 最后一个手风琴项
    &:last-child {
      border-bottom: none;
      
      // 最后一个项目如果没有展开，底部有圆角
      &:not([open]) summary {
        border-radius: 0 0 var(--accordion-radius) var(--accordion-radius);
      }
      
      // 最后一个项目展开时，标题变成直角
      &[open] summary {
        border-radius: 0;
      }
      
      // 最后一个项目的内容区域底部有圆角
      .cherry-detail-body {
        border-radius: 0 0 var(--accordion-radius) var(--accordion-radius);
      }
    }

    // 中间的项目保持直角
    &:not(:first-child):not(:last-child) {
      summary {
        border-radius: 0;
      }
      
      .cherry-detail-body {
        border-radius: 0;
      }
    }

    &:hover {
      box-shadow: none;
    }
  }
}

.cherry-text-align__left {
  .cherry-table-figure {
    display: flex;
    justify-content: flex-start;
  }
}

.cherry-text-align__center {
  table {
    margin-left: auto;
    margin-right: auto;
  }

  .cherry-table-figure {
    display: flex;
    justify-content: center;
  }
}

.cherry-text-align__right {
  table {
    margin-left: auto;
  }

  .cherry-table-figure {
    display: flex;
    justify-content: flex-end;
  }
}

.cherry-text-align__justify {
  .cherry-table-figure {
    display: flex;
    justify-content: flex-start; // 对于单个块状元素，回退到默认的左对齐
  }
}

.cherry-panel {
  margin: var(--panel-margin, 10px 0);
  overflow: hidden;
  border-radius: var(--panel-border-radius, var(--radius-xl));
  box-sizing: border-box;
  border: var(--panel-border, 0.5px solid var(--panel-border-color, transparent));
  background: var(--panel-bg, transparent);
  box-shadow: var(--panel-box-shadow, none);
  backdrop-filter: var(--panel-backdrop-filter, none);

  .cherry-panel--title {
    color: var(--panel-title-color, #fff);
    padding: var(--panel-title-padding, 5px 20px);
    background: var(--panel-title-bg, transparent);
    border-radius: var(--panel-title-border-radius, 0);
    border-bottom: var(--panel-title-border-bottom, none);

    &.cherry-panel--title__not-empty::before {
      font-family: 'ch-icon';
      margin: 0 var(--spacing-md) 0 -6px;
      vertical-align: bottom;
      content: var(--panel-icon, '');
    }
  }

  .cherry-panel--body {
    padding: var(--panel-body-padding, 5px 20px);
    background: var(--panel-body-bg, transparent);
    color: var(--panel-body-color, inherit);
    border-radius: var(--panel-body-border-radius, 0);
  }
}

.cherry-panel__primary {
  --panel-bg: var(--panel-primary-bg, #cfe2ff);
  --panel-border-color: var(--panel-primary-border-color, transparent);
  --panel-box-shadow: var(--panel-primary-box-shadow, none);
  --panel-title-bg: var(--panel-primary-title-bg, #0d6dfe);
  --panel-title-color: var(--panel-primary-title-color, #fff);
  --panel-title-border-radius: var(--panel-primary-title-border-radius, 0);
  --panel-title-border-bottom: var(--panel-primary-title-border-bottom, none);
  --panel-body-bg: var(--panel-primary-body-bg, transparent);
  --panel-body-color: var(--panel-primary-body-color, #0a58ca);
  --panel-body-border-radius: var(--panel-primary-body-border-radius, 0);
  --panel-icon: var(--panel-primary-icon, '\EA6A');
}

.cherry-panel__info {
  --panel-bg: var(--panel-info-bg, #cff4fc);
  --panel-border-color: var(--panel-info-border-color, transparent);
  --panel-box-shadow: var(--panel-info-box-shadow, none);
  --panel-title-bg: var(--panel-info-title-bg, #099cba);
  --panel-title-color: var(--panel-info-title-color, #fff);
  --panel-title-border-radius: var(--panel-info-title-border-radius, 0);
  --panel-title-border-bottom: var(--panel-info-title-border-bottom, none);
  --panel-body-bg: var(--panel-info-body-bg, transparent);
  --panel-body-color: var(--panel-info-body-color, #087990);
  --panel-body-border-radius: var(--panel-info-body-border-radius, 0);
  --panel-icon: var(--panel-info-icon, '\EA69');
}

.cherry-panel__warning {
  --panel-bg: var(--panel-warning-bg, #fff3cd);
  --panel-border-color: var(--panel-warning-border-color, transparent);
  --panel-box-shadow: var(--panel-warning-box-shadow, none);
  --panel-title-bg: var(--panel-warning-title-bg, #b38806);
  --panel-title-color: var(--panel-warning-title-color, #fff);
  --panel-title-border-radius: var(--panel-warning-title-border-radius, 0);
  --panel-title-border-bottom: var(--panel-warning-title-border-bottom, none);
  --panel-body-bg: var(--panel-warning-body-bg, transparent);
  --panel-body-color: var(--panel-warning-body-color, #997404);
  --panel-body-border-radius: var(--panel-warning-body-border-radius, 0);
  --panel-icon: var(--panel-warning-icon, '\EA6B');
}

.cherry-panel__danger {
  --panel-bg: var(--panel-danger-bg, #f8d7da);
  --panel-border-color: var(--panel-danger-border-color, transparent);
  --panel-box-shadow: var(--panel-danger-box-shadow, none);
  --panel-title-bg: var(--panel-danger-title-bg, #dc3545);
  --panel-title-color: var(--panel-danger-title-color, #fff);
  --panel-title-border-radius: var(--panel-danger-title-border-radius, 0);
  --panel-title-border-bottom: var(--panel-danger-title-border-bottom, none);
  --panel-body-bg: var(--panel-danger-body-bg, transparent);
  --panel-body-color: var(--panel-danger-body-color, #b02a37);
  --panel-body-border-radius: var(--panel-danger-body-border-radius, 0);
  --panel-icon: var(--panel-danger-icon, '\EA68');
}

.cherry-panel__success {
  --panel-bg: var(--panel-success-bg, #d1e7dd);
  --panel-border-color: var(--panel-success-border-color, transparent);
  --panel-box-shadow: var(--panel-success-box-shadow, none);
  --panel-title-bg: var(--panel-success-title-bg, #198754);
  --panel-title-color: var(--panel-success-title-color, #fff);
  --panel-title-border-radius: var(--panel-success-title-border-radius, 0);
  --panel-title-border-bottom: var(--panel-success-title-border-bottom, none);
  --panel-body-bg: var(--panel-success-body-bg, transparent);
  --panel-body-color: var(--panel-success-body-color, #146c43);
  --panel-body-border-radius: var(--panel-success-body-border-radius, 0);
  --panel-icon: var(--panel-success-icon, '\EA67');
}
